from django.db import models
from django.utils import timezone
from library.models import Book
from library.models import *

class User(models.Model):
    operation_choices = (
        ('0', '否'),
        ('1', '是'),
    )
    sex_choices = (
        ('1', '男'),
        ('2', '女'),
        ('0', '未知'),
    )
    subscribe_choices = (
        ('0', '未关注'),
        ('1', '已关注'),
    )
    language_choices = (
        ('zh_CN', '汉语'),
        ('en', '英语'),
    )
    subscribe = models.CharField(u'是否关注', max_length=1, blank=True, default='0',choices=subscribe_choices)  # 0表示没有关注，1表示关注
    openid = models.CharField(u'openid', max_length=50, default='')
    nickname = models.CharField(u'用户昵称', max_length=100, default='')
    name = models.CharField(u'用户姓名', max_length=50, blank=True, default='')
    headimgurl = models.URLField(u'用户头像', default='')
    sex = models.CharField(u'性别', max_length=1, default='0', choices=sex_choices) # 1表示男性，2表示女生，0表示未知
    language = models.CharField(u'用户语言', max_length=20, blank=True, default='', choices=language_choices)
    city = models.CharField(u'用户城市', max_length=50, blank=True, default='')
    province = models.CharField(u'用户省份', max_length=50, blank=True, default='')
    country = models.CharField(u'用户国家', max_length=50, blank=True, default='')
    subscribe_time = models.DateTimeField(u'关注时间', default=timezone.now)
    unionid = models.CharField(u'微信开发者标志', max_length=50, blank=True, default='')
    remark = models.CharField(u'用户在此公众号的备注', max_length=100, blank=True, default='')
    groupid = models.CharField(u'用户所在分组ID', max_length=20, blank=True, default='')
    tagid_list = models.CharField(u'用户标签ID列表', max_length=50, blank=True, default='')
    phonenumber = models.CharField(u'手机号', max_length=20, blank=True, default='')
    operation = models.CharField(u'是否为管理员', max_length=1, default='0', choices=operation_choices)  # 0为否，1为是

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'



class Borrow(models.Model):
    status_choice = (
        ('1', '在读'),
        ('2', '已还'),
    )
    book = models.ForeignKey(Book, on_delete=models.SET_DEFAULT, blank=True, default=1, verbose_name=u'书籍')
    user = models.ForeignKey(User, on_delete=models.SET_DEFAULT, blank=True, default=1, verbose_name=u'用户')
    borrow_time = models.DateTimeField(u'借书时间', default=timezone.now)
    return_time = models.DateTimeField(u'还书时间', blank=True, default=timezone.now)
    status = models.CharField(u'借阅状态', max_length=2, default='1', choices=status_choice)  # 1表示在读，2表示已还

    def __str__(self):
        return self.user.name + '----' + self.book.name

    class Meta:
        verbose_name = '借阅列表'
        verbose_name_plural = '借阅列表'


# 借书单
class BookBill(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=u'用户')
    book = models.ForeignKey(Book, on_delete=models.CASCADE, verbose_name=u'图书')

    class Meta:
        verbose_name = '借书单'
        verbose_name_plural = '借书单'